package com.heima.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heima.common.PageResult;
import com.heima.mapper.TaskStatusTypeMapper;
import com.heima.pojo.*;
import com.heima.pojo.dto.JobDto;
import com.heima.pojo.dto.TaskDto;
import com.heima.pojo.dto.UndoReasonDto;
import com.heima.pojo.vo.TaskCollectVo;
import com.heima.pojo.vo.TaskVo;
import com.heima.pojo.vo.UserWorkVo;
import com.heima.service.IJobService;
import com.heima.service.ITaskService;
import com.heima.service.IUndoReasonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/task-service/task")
@Api(tags = "工单管理")
public class TaskController {
    @Autowired
    private ITaskService taskService;
    @Autowired
    private TaskStatusTypeMapper taskStatusTypeMapper;
    @Autowired
    private IJobService jobService;
    @Autowired
    private IUndoReasonService undoReasonService;

    @GetMapping("/taskReportInfo/{start}/{end}")
    @ApiOperation("工单统计")
    public List<TaskReportInfo> taskReportInfo(@PathVariable String start, @PathVariable String end) {
        return taskService.taskReportInfo(start, end);
    }

    @GetMapping("/search")
    @ApiOperation("工单查询")
    public PageResult<TaskVo> search(PageResult<TaskVo> pageResult, @ApiParam(value = "开始时间") String start,
                                     @ApiParam(value = "结束时间") String end,
                                     @ApiParam(value = "设备编号") String innerCode,
                                     @ApiParam(value = "执行人Id") Integer userId,
                                     @ApiParam(value = "工单编号") String taskCode,
                                     @ApiParam(value = "工单状态") String status,
                                     @ApiParam(value = "是否是维修工单") Boolean isRepair) {
        return taskService.search(pageResult, start, end, innerCode, userId, taskCode, status, isRepair);
    }


    @GetMapping("/allTaskStatus")
    @ApiOperation("获取所有工单状态")
    public List<TaskStatusType> allTaskStatus() throws SQLException {
        return taskStatusTypeMapper.selectList(null);
    }


    @PostMapping("/create")
    @ApiOperation("工单创建")
    //TODO 添加工单 待完善
    public Boolean create(@RequestBody TaskDto taskDto) {
        return taskService.create(taskDto);
    }

    @PostMapping("/cancel/{taskId}")
    @ApiOperation("工单取消")
    public Boolean cancel(@PathVariable Long taskId, @RequestBody UndoReasonDto undoReasonDto) {
        LambdaUpdateWrapper<Task> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Task::getTaskStatus, 3).set(ObjectUtil.isNotEmpty(undoReasonDto.getDescribe()), Task::getDesc, undoReasonDto.getDescribe())
                .eq(Task::getTaskId, taskId);
        return taskService.update(updateWrapper);
    }

    @GetMapping("/taskInfo/{taskId}")
    @ApiOperation("工单详情")
    public TaskVo taskInfo(@PathVariable Long taskId) {
        return taskService.taskInfo(taskId);
    }

    @GetMapping("/supplyAlertValue")
    @ApiOperation("获取自动补货工单阈值")
    public Integer supplyAlertValue() {
        Job job = jobService.getOne(null);
        return job.getAlertValue();
    }

    @PostMapping("/autoSupplyConfig")
    @ApiOperation("设置自动补货工单阈值")
    public Boolean autoSupplyConfig(@RequestBody JobDto jobDto) {
        if (ObjectUtil.isEmpty(jobDto)) {
            return false;
        }
        LambdaUpdateWrapper<Job> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Job::getAlertValue, jobDto.getAlertValue());
        return jobService.update(updateWrapper);
    }

    @GetMapping("/collectReport/{start}/{end}")
    @ApiOperation("工单状态统计")
    public List<CollectReport> collectReport(@PathVariable String start, @PathVariable String end) {
        return taskService.collectReport(start, end);
    }

    @GetMapping("/userWorkTop10/{start}/{end}/{isRepair}/{regionId}")
    @ApiOperation("获取用户工单top10")
    public List<Map<String, Object>> userWorkTop10(@PathVariable String start, @PathVariable String end, @PathVariable Boolean isRepair, @PathVariable Long regionId) {
        return taskService.userWorkTop10(start, end, isRepair, regionId);
    }

    @GetMapping("/userWork")
    @ApiOperation("获取用户工单")
    public UserWorkVo userWork(String start, String end, Integer userId) {
        return taskService.userWork(start, end, userId);
    }
}
